package com.example.dell.fastshopping.DuoBao;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.fastshopping.DuoBao.model.DuoBaoActViewModel;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.mall.entity.ServerTime;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.xml.validation.Validator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ${YSH} on 2019/5/1.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class DuoBaoListFragment extends Fragment {

    @BindView(R.id.rv)
    RecyclerView rv;
    private DuoBaoActViewModel viewModel;
    private Context mContext;
    private Unbinder bind;
    private List<ServerTime.DuobaoHistory> duobaolist;

    public static DuoBaoListFragment newInstance() {
        return new DuoBaoListFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_duobao_list, container, false);
        bind = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = ViewModelProviders.of(getActivity()).get(DuoBaoActViewModel.class);
        initView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    private void initView() {
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        viewModel.getHistoryList().observe(this, new Observer<List<ServerTime.DuobaoHistory>>() {
            @Override
            public void onChanged(@Nullable List<ServerTime.DuobaoHistory> duobaoHistories) {
                duobaolist = duobaoHistories;
                if (duobaolist == null) {
                    duobaolist = new ArrayList<>();
                }
                if (rv.getAdapter() == null) {
                    rv.setAdapter(new MyAdapter());
                } else {
                    rv.getAdapter().notifyDataSetChanged();
                }
            }
        });

    }


    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {


        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_duobao_act, viewGroup, false);
            MyHolder myHolder = new MyHolder(view);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            ServerTime.DuobaoHistory duobaoHistory = duobaolist.get(i);
            myHolder.lun.setText("第" + duobaoHistory.getRound() + "轮");
            myHolder.userName.setText("用户" + duobaoHistory.getUserName());
            myHolder.price.setText("￥" + duobaoHistory.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            myHolder.status.setText(duobaoHistory.getStatus() == 0 ? "抢拍失败" : "抢拍成功");
            myHolder.fanli.setText(duobaoHistory.getBalanceString());
            if (duobaoHistory.getStatus() == 0) {
                int gray = mContext.getResources().getColor(R.color.myGrey);
                myHolder.lun.setTextColor(gray);
                myHolder.userName.setTextColor(gray);
                myHolder.price.setTextColor(gray);
                myHolder.status.setTextColor(gray);
                myHolder.userIcon.setImageResource(R.mipmap.user_gray);
                myHolder.fanli.setVisibility(View.GONE);
            } else {
                int black = mContext.getResources().getColor(R.color.text_black);
                myHolder.lun.setTextColor(black);
                myHolder.userName.setTextColor(black);
                myHolder.price.setTextColor(black);
                myHolder.status.setTextColor(black);
                myHolder.userIcon.setImageResource(R.mipmap.user_black);
                myHolder.fanli.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public int getItemCount() {
            return duobaolist.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder {

        public TextView userName;
        public TextView lun;
        public TextView price;
        public TextView status;
        public TextView fanli;
        public ImageView userIcon;

        public MyHolder(View view) {
            super(view);
            userName = view.findViewById(R.id.user_name);
            userIcon = view.findViewById(R.id.user_icon);
            lun = (TextView) view.findViewById(R.id.lun);
            price = view.findViewById(R.id.price);
            fanli = view.findViewById(R.id.fanli);
            status = view.findViewById(R.id.status);
        }
    }
}
